
import { getEntireRoomList } from '@/services'
import * as actionTypes from './constants'

export const changeCurrentPageAction = (currentPage)=>({
  type: actionTypes.CHANGE_CURRENT_PAGE,
  currentPage
})

export const changeRoomListAction = (roomList) => ({
  type: actionTypes.CHANGE_ROOM_LIST,
  roomList
})

export const changeTotalCountAction = (totalCount) => ({
  type: actionTypes.CHANGE_TOTAL_COUNT,
  totalCount
})

export const changeIsLoadingAction = (isLoading) => ({
  type: actionTypes.CHANGE_IS_LOADING,
  isLoading
})



export const fetchEntireDataAction = (page=0)=>{
  return async (dispatch, getState)=>{
    // 根据传入的page修改页码
    dispatch(changeCurrentPageAction(page))
    dispatch(changeIsLoadingAction(true))
    // 根据页码调用接口获取列表数据
    const res = await getEntireRoomList({offset: page*20, size:20})
    dispatch(changeIsLoadingAction(false))
    if (res.errcode ===0){  
      // 保存redux
      dispatch(changeRoomListAction(res.list))
      dispatch(changeTotalCountAction(res.totalCount))
    } else {
      // throw new Error("报错")
    }
  }
}